package com.songlanyun.modules.ad.controller.admin;


import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.ad.entity.AdEntity;
import com.songlanyun.modules.ad.service.AdService;
import com.songlanyun.modules.exception.AdException;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 广告表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-11 13:55:19
 */
@RestController
@RequestMapping("admin/ad/ad")
public class AdAdminController extends AbstractController {
    @Autowired
    private AdService adService;

    /**
     * 列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("ad:ad:list")
    public R list(@RequestParam Map<String, Object> params) {

        params.put("shopId", getShopId());//平台查询平台广告，商家查询商家店铺

        PageUtils page = adService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 根据广告查询对应广告和广告位信息
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("ad:ad:info")
    public R info(@PathVariable("id") Integer id) {
        AdEntity ad = adService.getInfo(id);

        return R.ok().put("data", ad);
    }

    /**
     * 保存/修改
     */
    @SysLog("保存修改广告")
    @PostMapping("/save")
//    @RequiresPermissions("ad:ad:save")
    public R save(@RequestBody AdEntity ad) {
        ValidatorUtils.validateEntity(ad);
        ad.setShopId(getShopId());//设置商家id
        if (ad.getId() != null)  {
            //修改 校验用户，
            verifyShopId(ad.getShopId());
        }
        adService.saveOrUpdate(ad);
        return R.ok();
    }


    /**
     * 上下线
     */
    @SysLog("广告上下线")
    @PostMapping("/onOff")
//    @RequiresPermissions("ad:ad:save")
    public R onOff(@RequestBody AdEntity ad) {
        if (ad.getOnOff() == null) {
            throw new RRException(AdException.PARAMETER_NOT_NUll);
        }
        AdEntity adE = adService.getById(ad.getId(), true);
        verifyShopId(adE.getShopId());

        adE.setOnOff(ad.getOnOff());
        adService.updateById(adE);


        return R.ok();
    }


    /**
     * 置顶
     */
    @SysLog("广告置顶")
    @PostMapping("/top")
//    @RequiresPermissions("ad:ad:save")
    public R top(@RequestBody AdEntity ad) {
        if (ad.getIsTop() == null) {
            throw new RRException(AdException.PARAMETER_NOT_NUll);
        }
        AdEntity adE = adService.getById(ad.getId(), true);
        verifyShopId(adE.getShopId());

        adE.setIsTop(ad.getIsTop());//置顶
        if (ad.getIsTop()) {
            adE.setTopTime(new Date());//设置置顶时间
        }
        adService.updateById(adE);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("广告删除")
    @DeleteMapping("/delete/{id}")
//    @RequiresPermissions("ad:ad:delete")
    public R delete(@PathVariable("id") Integer id) {
        adService.removeById(id);
        return R.ok();
    }



}
